#pragma once
#include <functional>
#include <memory>
#include <SDL2/SDL_ttf.h>
#include <string>
#include <unordered_map>
#include <utility>

namespace engine
{
    namespace resource
    {
        using FontKey = std::pair<std::string, int>;
        struct FontKeyHash
        {
            std::size_t operator()(const FontKey& key) const noexcept
            {
                std::hash<std::string> string_hash;
                std::hash<int> int_hash;
                return string_hash(key.first) ^ (int_hash(key.second));
            }
        };
        class FontManager
        {
			friend class ResourceManager;
        public:
            struct SDLFontDeleter
            {
                void operator()(const TTF_Font* font)
                {
                    if (font)
                    {
                        TTF_CloseFont(const_cast<TTF_Font*>(font));
                    }
                }
            };

            FontManager();
            ~FontManager();

            FontManager(const FontManager&) = delete;
            FontManager& operator=(const FontManager&) = delete;
            FontManager(FontManager&&) = delete;
            FontManager& operator=(FontManager&&) = delete;

        private:
			std::unordered_map<FontKey, std::unique_ptr<TTF_Font, SDLFontDeleter>, FontKeyHash> m_fonts; //字体资源

			TTF_Font* loadFont(const std::string& filePath, int fontSize); //加载字体
			TTF_Font* getFont(const std::string& filePath, int fontSize);   //获取字体
			void unloadFont(const std::string& filePath, int fontSize);          //卸载字体
			void clearFonts();                                                     //清除所有字体
        };
    }
}
     